﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    public class Tbl_sanpham_DAL
    {
        public SqlConnection cn = null;
        public Tbl_sanpham_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public Tbl_sanpham_DAL(Tbl_sanpham_DAL sp)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public Tbl_sanpham_Collection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("GET_SANPHAM", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            Tbl_sanpham_Collection spCollection = new Tbl_sanpham_Collection();

            while (dr.Read())
            {
                Tbl_sanpham_Entities sp = new Tbl_sanpham_Entities();
                sp.STT = int.Parse(dr.GetValue(0).ToString());
                sp.MaSP = dr.GetValue(1).ToString();
                sp.TenSP = dr.GetValue(2).ToString();
                sp.GiaGoc = float.Parse(dr.GetValue(3).ToString());
                sp.GiaBan = float.Parse(dr.GetValue(4).ToString());
                sp.TyLeGiamGia = float.Parse(dr.GetValue(5).ToString());
                sp.NgayDangSP = dr.GetValue(6).ToString();
                sp.ThoiGianSuDung = dr.GetValue(7).ToString();
                sp.ThongTinSanPham = dr.GetValue(8).ToString();
                sp.DieuKienSuDung = dr.GetValue(9).ToString();
                sp.SoLuongBan = int.Parse(dr.GetValue(10).ToString());
                sp.STT_doanhnghiep = int.Parse( dr.GetValue(11).ToString());
                sp.TinhTrang = dr.GetValue(12).ToString();
                sp.LoaiSP =  int.Parse(dr.GetValue(13).ToString());

                spCollection.Add(sp);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return spCollection;
        }

        public int insert(Tbl_sanpham_Entities sp)
        {
            SqlCommand cm = new SqlCommand("INSERT_SANPHAM", cn);
            cm.CommandType = CommandType.StoredProcedure;
           
            cm.Parameters.AddWithValue("@MASP", sp.MaSP);
            cm.Parameters.AddWithValue("@TENSP", sp.TenSP);
            cm.Parameters.AddWithValue("@GIAGOC", sp.GiaGoc);
            cm.Parameters.AddWithValue("@GIABAN", sp.GiaBan);
            cm.Parameters.AddWithValue("@TYLEGIAMGIA", sp.TyLeGiamGia);
            cm.Parameters.AddWithValue("@NGAYDANGSP", sp.NgayDangSP);
            cm.Parameters.AddWithValue("@THOIGIANSUDUNG", sp.ThoiGianSuDung);
            cm.Parameters.AddWithValue("@ThongTinSanPham", sp.ThongTinSanPham);
            cm.Parameters.AddWithValue("@DIEUKIENSUDUNG", sp.DieuKienSuDung);
            cm.Parameters.AddWithValue("@SOLUONGBAN", sp.SoLuongBan);
            cm.Parameters.AddWithValue("@STT_DOANHNGHIEP", sp.STT_doanhnghiep);
            cm.Parameters.AddWithValue("@TINHTRANG", sp.TinhTrang);
            cm.Parameters.AddWithValue("@LOAISP", sp.LoaiSP);
            cm.Parameters.Add(new SqlParameter("@STT", DbType.Int32)).Direction = ParameterDirection.Output;
            cm.ExecuteNonQuery();
            int stt = int.Parse(cm.Parameters["@STT"].Value.ToString());
            return stt;
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("DELETE_SANPHAM", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", iD);
            cm.ExecuteNonQuery();
        }
        public void Update(Tbl_sanpham_Entities sp)
        {
            SqlCommand cm = new SqlCommand("UPDATE_SANPHAM", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", sp.STT);
            cm.Parameters.AddWithValue("@MASP", sp.MaSP);
            cm.Parameters.AddWithValue("@TENSP", sp.TenSP);
            cm.Parameters.AddWithValue("@GIAGOC", sp.GiaGoc);
            cm.Parameters.AddWithValue("@GIABAN", sp.GiaBan);
            cm.Parameters.AddWithValue("@TYLEGIAMGIA", sp.TyLeGiamGia);
            cm.Parameters.AddWithValue("@NGAYDANGSP", sp.NgayDangSP);
            cm.Parameters.AddWithValue("@THOIGIANSUDUNG", sp.ThoiGianSuDung);
            cm.Parameters.AddWithValue("@THONGTINSP", sp.ThongTinSanPham);
            cm.Parameters.AddWithValue("@DIEUKIENSUDUNG", sp.DieuKienSuDung);
            cm.Parameters.AddWithValue("@SOLUONGBAN", sp.SoLuongBan);
            cm.Parameters.AddWithValue("@STT_DOANHNGHIEP", sp.STT_doanhnghiep);
            cm.Parameters.AddWithValue("@TINHTRANG", sp.TinhTrang);
            cm.Parameters.AddWithValue("@LOAISP", sp.LoaiSP);
            cm.ExecuteNonQuery();
        }

    }
}
